<?php

class sliderimage extends baseController {
	function index() {
		if ( null == SessionUtil::getVar('slider_image_show_setting') ) {
			SessionUtil::setVar('slider_image_show_setting','Y');
		}
		$this->show_setting = SessionUtil::getVar('slider_image_show_setting');
		
		$slider_type = $this->getCurrentSliderImageType();
		$conditions = array('type'=>$slider_type);
		if ( 'all' == $this->show_setting ) {
		
		} else if ( 'Y' == $this->show_setting ) {
			$conditions['active'] = 'Y';
		} else if ( 'N' == $this->show_setting ) {
			$conditions['active'] = 'N';
		}
		$slider_image_db_obj = spClass("model_slider_image");
		$this->slider_image_list = $slider_image_db_obj->findAll($conditions,'`order` ASC');
		
		$resource_obj = spClass("model_resource");
		
		$tmp_slider_image_list = array();
		//load image path from resource object
		$item_index = 1;
		foreach ( $this->slider_image_list as $row_data_obj) {
			if ( strlen($row_data_obj['resource_uuid']) > 0 ) {
				$resource_data_row = $resource_obj->get($row_data_obj['resource_uuid']);
				$row_data_obj['img_path'] = 'upload/assets/'.$resource_data_row['path'];
				
				$size_info = '';
				$disp_file_path = ASSETS_DIR.'/'.$resource_data_row['path'];
				$image_size_info = getimagesize($disp_file_path);
				$size_info = sprintf("尺寸：%d X %d <br>格式: %s<br>",$image_size_info[0],$image_size_info[1],$image_size_info['mime']);
		
				
				$file_size = filesize($disp_file_path);
				$file_size /= 1000;
				$size_info .= "大小：$file_size Kb";
				
				$row_data_obj['size_info'] = $size_info;
				
			}
			$row_data_obj['item_index'] = $item_index;
			$tmp_slider_image_list[] = $row_data_obj;
			$item_index++;
		}
		$this->slider_image_list = $tmp_slider_image_list;

	}
	function edit() {
		
		$uuid = $this->spArgs ( "uuid" );
		$slider_image_db_obj = spClass ( "model_slider_image" );
		$active = $this->spArgs ( "active" );
		//Post form
		if (strlen ( $active ) > 0) {
			$exist_data = $slider_image_db_obj->get ( $uuid );
			$is_new = FALSE;
			if (! $exist_data) {
				$exist_data = array('type' => $this->getCurrentSliderImageType());
				$is_new = TRUE;
			}
			$slider_image_sort = intval($this->spArgs ( "sort" ));
			$ajaxf_image = $this->spArgs ( "ajaxf_image_value" );
			$slider_image_active = $this->spArgs ( "active" );
			
			$ftp_file_name = $this->spArgs ( "ftp_file_name" );
			if (	$is_new
					or $exist_data ['order'] != $slider_image_sort
					or $exist_data ['active'] != $slider_image_active
					or strlen ( $ajaxf_image ) > 0
					or strlen($ftp_file_name) > 0) {
				$exist_data ['order'] = $slider_image_sort;
				$exist_data ['active'] = $slider_image_active;
				$tmp_file_path = FALSE;
				if ( strlen($ajaxf_image) > 0) {
			
					$tmp_file_path =  UPLOAD_TMP_DIR.'/' . $ajaxf_image;
				} elseif ( strlen($ftp_file_name) > 0) {
					$tmp_file_path = 'ftp/'.$ftp_file_name;
				}
				if ( $tmp_file_path ) {
					$resource_obj = spClass ( "model_resource" );
					if (strlen ( $exist_data ['resource_uuid'] ) > 0) {
						$resource_obj->updateByTmpFile ( $tmp_file_path, $exist_data ['resource_uuid'] );
					} else {
						$new_resource_uuid = $resource_obj->createByTmpFile ( $tmp_file_path );
						$exist_data ['resource_uuid'] = $new_resource_uuid;
					}
				}
				if ( $is_new ) {
					$slider_image_db_obj->create ( $exist_data );
				} else {
					$slider_image_db_obj->update ( array ($slider_image_db_obj->pk => $exist_data [$slider_image_db_obj->pk] ), $exist_data );
				}
			}
			$this->jump ( spUrl ( 'sliderimage', 'index' ) );
		}
		
		$slider_image_data = $slider_image_db_obj->get ( $uuid );
		if ( !$slider_image_data ) {
			$slider_image_data = array('active'=>'Y');
		}
		$this->slider_imae_data = $slider_image_data;
		
		$this->session_id = session_id();
		$this->session_name = session_name();
		//ftp file list
		$fpt_file_list = array();
		$dp = opendir("ftp");
		//List files in images directory
		while (($file_name = readdir($dp)) !== false)
		{
			if ( $file_name == '.' OR $file_name == '..' )
				continue;
			$ext = Util::getFileExt($file_name);
			if ( strtolower($ext) == 'jpg' 	OR
					strtolower($ext) == 'jpeg' 	OR
					strtolower($ext) == 'png' OR
					strtolower($ext) == 'gif' ) {
				$file_size = filesize('ftp/'.$file_name);
				$file_size /= 1000;
				$fpt_file_list[] = array('name'=>$file_name,'size'=>sprintf("%0.2f Kb",$file_size));
			}
		}
		closedir($dp);
		$this->fpt_file_list = $fpt_file_list;
	}
	function delete() {
		$uuid = $this->spArgs ( "uuid" );
		$slider_image_db_obj = spClass ( "model_slider_image" );
		$slider_image_db_obj->model_version_delete_byPk ( $uuid, true, true );
	
		$this->jump ( spUrl ( 'sliderimage', 'index' ) );
	}
	function setshowsetting(){
		$value = $this->spArgs("v");
		SessionUtil::setVar('slider_image_show_setting',$value);
		$this->jump(spUrl('sliderimage','index'));
	}
	
	private function getCurrentSliderImageType() {
		return SessionUtil::getVar('current_slider_image_type');
	}
}

?>